<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>indexedDB</title>
</head>
<body>
<script>
    var db=window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB

    var request, result, version=5,
        dbName='testDB',
        osName='os1'

    function createDB(){
        request = db.open(dbName,version)
        request.onsuccess=function(){
            db=request.result
            console.log('open success')
        }
        request.onerror=function(e){
            console.log(e.currentTarget.error.message)
        }
        request.onupgradeneeded=function(){
            var store = null
            db=request.result
            if(!db.objectStoreNames.contains(osName)){
                store = db.createObjectStore(osName,{keyPath:'id'})
                store.createIndex('idIndex','id',{unique:true})
                store.createIndex('hpIndex','hp',{unique:false})
                store.createIndex('categoryIndex','category',{multiEntry:true})
            }
        }
    }
    createDB()

    // save data
    var data=[{
        name:'史莱姆',
        id:'001',
        hp:3,
        category:['怪物','容易逃跑']
    },{
        name:'小蝙蝠',
        id:'002',
        hp:5,
        category:['飞','容易逃跑']
    },{
        name:'小恶魔',
        id:'003',
        hp:9,
        category:['飞','不好看']
    }]
    function addData () {
        var transaction=db.transaction(osName,'readwrite'),
        store=transaction.objectStore(osName)
        data.map(function (o) {
            store.add(o)
        })
    }
    function getData(id){
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        var request=store.get(id)
        request.onsuccess=function () {
            console.log(request.result)
        }
    }
    function getAllData() {
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        var request=store.getAll()
        request.onsuccess=function () {
            console.log(request.result)
        }
    }
    function updateData(id){
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        var request=store.get(id)
        request.onsuccess=function () {

            request=store.put({
                name:'小恶魔2',
                id:id,
                hp:10
            })
        }
    }
    function deleteData(id){
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        var request=store.delete(id)
        request.onsuccess=function () {
            console.log('delete success')
        }
    }
    function deleteData(id){
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        var request=store.delete(id)
        request.onsuccess=function () {
            console.log('delete success')
        }
    }
    function clear() {
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        var request=store.clear()
        request.onsuccess=function () {
            console.log('clear success')
        }
    }
    function useCursorGetData(){
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        request = store.openCursor(null,'prev')
        // request = store.openCursor(IDBKeyRange.only('002'))
        request.onsuccess=function(){
            var cursor = request.result
            if(cursor){
                console.log(cursor.value)
                cursor.continue()
            }
        }
    }
    function useIndexGetData(){
        var transaction=db.transaction(osName,'readwrite'),
            store=transaction.objectStore(osName)
        index = store.index('hpIndex'),
            request=index.openCursor(IDBKeyRange.bound(5,10,true,true))

        request.onsuccess=function(){
            var cursor = request.result, value= null
            if(cursor){
                value=cursor.value
                value.hp+=20
                cursor.update(value)
                console.log(cursor.value)
                cursor.continue()
            }

        }

    }
</script>
</body>
</html>